package com.sh.data.engine.web.webapi.controller.normalization;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.normalization.standard.*;
import com.sh.data.engine.application.model.request.normalization.*;
import com.sh.data.engine.application.service.normalization.DictionaryMappingApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.normalization.standard.service.IDictionaryMappingService;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jingchen.hr
 */
@Tag(name = "规范设计-字典映射", description = "规范设计-字典映射API")
@RequestMapping("/mapping")
@RestController
@Slf4j
public class DictionaryMappingController {
    @Autowired
    private IDictionaryMappingService dictionaryMappingService;

    @Autowired
    private DictionaryMappingApplicationService dictionaryMappingApplicationService;

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZYS, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "字典映射列表", description = "字典映射列表")
    @GetMapping("/list")
    public RestResponseEntity<PagerResponse<DictionaryMappingResponse>> listDictionaryMapping(
        @Validated @ModelAttribute DictionaryQueryRequest request) {
        var result = dictionaryMappingApplicationService.listDictionaryMapping(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZYS, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "字典映射详情列表", description = "字典映射详情列表")
    @GetMapping("/detail/list")
    public RestResponseEntity<PagerResponse<DictionaryMappingDetailResponse>> detailListDictionaryMapping(
        @Validated @ModelAttribute DictionaryMappingDetailQueryRequest request) {
        var result = dictionaryMappingApplicationService.detailListDictionaryMapping(request);
        return RestResponseEntity.success(result);
    }

    //    @BizDataIndexAnnotation(
//        type = BizDataIndexTypeEnum.STANDARD,
//        dataIdFieldName = "id",
//        dataIdParamIndex = 0,
//        operateType = BizDataIndexAnnotation.OperateType.SAVE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZYS, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "绑定或解绑映射详情", description = "绑定或解绑映射详情")
    @PostMapping("/detail/bound")
    @LogExecution(operation = EngineOperation.BIND, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.BIND)
    public RestResponseEntity<Boolean> detailBound(
        @Validated @RequestBody MappingDetailRequest request) {
        var result = dictionaryMappingApplicationService.detailBound(request);
        return RestResponseEntity.success(result);
    }

    //    @BizDataIndexAnnotation(
//        type = BizDataIndexTypeEnum.STANDARD,
//        dataIdFieldName = "id",
//        dataIdParamIndex = 0,
//        operateType = BizDataIndexAnnotation.OperateType.SAVE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZYS, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "维护", description = "维护")
    @PostMapping("/detail/maintain")
    public RestResponseEntity<Boolean> detailMaintain(
        @Validated @RequestBody MappingDetailRequest request) {
        var result = dictionaryMappingApplicationService.detailMaintain(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZBG, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "映射报告类型信息", description = "映射报告类型信息")
    @GetMapping("/report/info")
    public RestResponseEntity<MappingReportInfoResponse> reportInfo() {
        var result = dictionaryMappingApplicationService.reportInfo();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取字典详情", description = "获取字典详情")
    @GetMapping("/dictionary/detail")
    public RestResponseEntity<List<GetDictionaryDetailResponse>> getDictionaryDetail(
        @Parameter(description = "模糊搜索") @RequestParam(value = "dictNameEn", required = false)
        String dictNameEn,
        @Parameter(description = "模糊搜索") @RequestParam(value = "dictNameCn", required = false)
        String dictNameCn) {
        var result = dictionaryMappingApplicationService.getDictionaryDetail(dictNameEn, dictNameCn);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "保存字典", description = "保存字典")
    @GetMapping("/dictionary/save")
    public RestResponseEntity<Boolean> saveDictionaryDetail() {

        return null;
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZBG, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "映射报告列表", description = "映射报告列表")
    @GetMapping("/report/list")
    public RestResponseEntity<PagerResponse<MappingReportResponse>> listReport(
        @Validated @ModelAttribute MappingReportDetailRequest request) {
        var result = dictionaryMappingApplicationService.listReport(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZBG, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "映射报告详情列表[命中字段查询]", description = "映射报告详情列表[命中字段查询]")
    @GetMapping("/report/detail/list")
    public RestResponseEntity<List<DictionaryMappingReportDetailResponse>> listReportDetail(
        @Validated @ModelAttribute MappingReportRequest request) {
        var result = dictionaryMappingApplicationService.listReportDetail(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZBG, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "表标准查询", description = "表标准查询")
    @GetMapping("/table/standard/list")
    public RestResponseEntity<List<TableStandardResponse>> tableStandardList(
        @Parameter(description = "表名", required = true) @RequestParam String tableName) {
        var result = dictionaryMappingApplicationService.tableStandardList(tableName);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZBG, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "映射报告导出", description = "映射报告导出")
    @GetMapping("/report/export/list")
    public void listReportExport(
        HttpServletResponse response, @Validated @ModelAttribute MappingReportDetailRequest request) {
        dictionaryMappingApplicationService.listReportExport(response, request);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZBG)
    @Operation(summary = "获取字典任务最后一次执行时间", description = "获取字典任务最后一次执行时间")
    @GetMapping("/dictionary/getLastTaskExecuteTime")
    public RestResponseEntity<String> getLastTaskExecuteTimeStamp() {
        var result = dictionaryMappingApplicationService.getLastTaskExecuteTimeStamp();
        return RestResponseEntity.success(result);
    }
}
